/**
 * 表单验证
 */
let nameVer = /^\S+$/;
let momoVer = /^.+[\s\S]*$/;
let input = $("#formModify :input");

function verification(regular,con){
    if (regular.test(con.value)) {
        $(con).parent().parent().removeClass('has-error');
        $(con).parent().parent().addClass('has-success');
        $(con).next().removeClass("glyphicon-remove form-control-feedback");
        $(con).next().addClass("glyphicon glyphicon-ok form-control-feedback");
    } else {
        $(con).parent().parent().removeClass('has-success');
        $(con).parent().parent().addClass('has-error');
        $(con).next().removeClass("glyphicon-remove form-control-feedback");
        $(con).next().addClass("glyphicon glyphicon-remove form-control-feedback");
    }
}
$(input).on("input porpertychange", function () {
    verification(momoVer, this);
})
$(input[1]).on("input porpertychange", function () {
    verification(nameVer, this);
})
$("#add :input").on("input porpertychange", function () {
    verification(momoVer, this);
});
$($("#add :input")[0]).on("input porpertychange blur", function () {
    verification(nameVer, this);
});

/**
 * 分页查询
 * @param num
 * @param size
 */

let pageBean = new Object();//存储分页信息的对象

findPage(1, 5);

function findPage(num, size) {
    pageBean.pageSize = size;
    pageBean.factor = [];
    if (num <= 1) {
        pageBean.pageNo = 1;
    } else if (num > Math.ceil($("#sum").html() / pageBean.pageSize)) {
        pageBean.pageNo = Math.ceil($("#sum").text() / pageBean.pageSize);
    } else {
        pageBean.pageNo = num;
    }
    if ($("#cageId").val() != "") {
        if ($("#cageId").val().indexOf("'") == -1) {
            pageBean.factor.push(" id='" + $("#cageId").val() + "'");
        } else {
            pageBean.factor.push(" 1=0 ");
        }
    }
    if ($("#cageName").val() != "") {
        if ($("#cageName").val().indexOf("'") == -1) {
            pageBean.factor.push(" name like '%" + $("#cageName").val() + "%' ");
        } else {
            pageBean.factor.push(" 1=0 ");
        }
    }

    //以上均为判断条件
    $.post(
        "/categoryController/show",
        pageBean,
        function (data) {
            $("#tb").empty();
            $("#ctn").empty();
            if (data.data != null) {
                let cgs = data.data;
                let tr = "", status;
                let btn = "<li><a href='javascript:findPage(pageBean.pageNo-1,pageBean.pageSize)' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>";
                for (let i = 0; i < data.totalPage; i++) {
                    btn += "<li><a href='#' onclick='findPage(this.innerText,pageBean.pageSize)'>" + (i + 1) + "</a></li>";
                }
                btn += "<li><a href='javascript:findPage(pageBean.pageNo+1,pageBean.pageSize)' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>";
                for (let i = 0; i < cgs.length; i++) {
                    if (cgs[i].status == 1) {
                        status = "<button class='btn btn-xs btn-info'>启用</button>";
                    } else {
                        status = "<button class='btn btn-xs btn-danger'>禁用</button>";
                    }
                    tr += "<tr><td>" + cgs[i].id + "</td><td>" + cgs[i].name + "</td><td>" + cgs[i].momo + "</td><td>" + status + "</td><td><button type='button' onclick='modify(this)' class='btn btn-info' data-toggle='modal' data-target='#myModal'>修改</button></td></tr>";
                }
                $("#tb").html(tr);//tboby内容
                $("#count").html(data.pageSize);//每页显示多少条数据
                $("#sum").html(data.rowCount);//总共多少条记录
                $("#start").html(data.startIndex + 1);//当前页从第几条记录开始
                $("#end").html(data.startIndex + data.length);//到第几条记录结束
                $("#ctn").html(btn);//导航条内容
                $("#ctn li:contains(" + pageBean.pageNo + ")").addClass("active");//激活当前所在页数导航条按钮
            } else {
                $("#start").html(data.startIndex);//当前页从第几条记录开始
                $("#end").html(0);//到第几条记录结束
                $("#sum").html(data.rowCount);//总共多少条记录
            }
        },
        "json"
    );

}

/**
 * 修改商品类别
 * @param btn 修改按钮
 */
function modify(btn) {
    let td = $(btn).parent().parent().children("td");
    $.post(
        "/categoryController/findId",
        {id: td[0].innerText},
        function (data) {
            input[0].value = data.id;
            input[1].value = data.name;
            input[2].value = data.status;
            input[3].value = data.momo;
        },
        "json"
    );
    /**
     * 修改商品类别模态框确认按钮
     */
    $("#confirm").on("click", function () {
        if ((nameVer.test(input[1].value)) && (nameVer.test(input[3].value))) {
            $.post(
                "/categoryController/modify",
                $("#formModify").serialize(),
                function (data, status) {
                    if (status == "success") {
                        let sta = "<button type='button' class='btn btn-xs btn-danger'>禁用</button>";
                        if (data.status == 1) {
                            sta = "<button type='button' class='btn btn-xs btn-info'>启用</button>"
                        }
                        td[0].innerText = data.id;
                        td[1].innerText = data.name;
                        td[2].innerText = data.momo;
                        td[3].innerHTML = sta;
                        toastr.success("操作成功");
                        $("#myModal").modal("hide");
                    } else {
                        toastr.error("操作失败");
                    }
                },
                "json"
            );
        } else {
            toastr.warning("请正确输入信息")
        }
    });

}

/**
 * 模态框消失清空状态并且解除绑定事件
 */
$(".modal").on("hide.bs.modal", function () {
    $("#add").get(0).reset();
    $("#confirm").off("click");
    $(".verification").removeClass("has-success has-error");
    $(".verification span").removeClass("glyphicon  glyphicon-ok glyphicon-remove");
})
/**
 * 增加商品分类
 */
$("#addBtn").click(function () {
    if ((nameVer.test($("#add :input")[0].value)) && (momoVer.test($("#add :input")[2].value))) {
        $.post(
            "/categoryController/add",
            $("#add").serialize(),
            function (data, status) {
                if (status == "success") {
                    toastr.success("添加成功");
                    $("#myModal2").modal("hide");
                    findPage(1, pageBean.pageSize);
                }
            },
            "json"
        );
    } else {
        toastr.warning("请检查输入的内容");
    }
});
